/**

 @Name：layuiAdmin 内容系统
 @Author：star1029
 @Site：http://www.layui.com/admin/
 @License：LPPL

 */


layui.define(['table', 'form'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form;

    //订单管理
    table.render({
        elem: '#LAY-app-content-list'
        , url: '/navigation/lists'
        , page: true
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', title: 'ID', sort: true}
            , {field: 'title', title: '标题', sort: true}
            , {field: 'images_url', title: '封面图片', width: 100, templet: '#imgTpl'}
            , {field: 'mold', title: '跳转类型'}
            , {field: 'link', title: '跳转链接'}
            , {title: '操作', minWidth: 150, align: 'center', fixed: 'right', toolbar: '#table-content-list'}
        ]]
        , skin: 'line'
        , limits: [10]
    });

    //监听工具条
    table.on('tool(LAY-app-content-list)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除？', function (index) {
                var data = obj.data;
                obj.del();
                layer.close(index);
                $.ajax({
                    url: "/navigation/delete",
                    data: {id: data.id},
                    type: "Post",
                    dataType: "json",
                    success: function (data) {
                        //table.reload('LAY-app-content-list');
                    },
                });

            });
        } else if (obj.event === 'edit') {
            layer.open({
                type: 2
                , title: '首页导航编辑'
                , content: '/navigation/edit/' + data.id
                , maxmin: true
                , area: ['800px', '550px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");

                    //监听提交
                    iframeWindow.layui.form.on('submit(layuiadmin-app-form-edit)', function (data) {
                        var field = data.field; //获取提交的字段

                        //提交 Ajax 成功后，静态更新表格中的数据
                        //提交 Ajax 成功后，关闭当前弹层并重载表格
                        $.ajax({
                            url: "/navigation/update",
                            data: field,
                            type: "Post",
                            dataType: "json",
                            success: function (data) {
                                if (data.code == 0) {
                                    layer.close(index); //关闭弹层
                                    table.reload('LAY-app-content-list');
                                } else {
                                    layer.msg(data.msg, {
                                        offset: '15px'
                                        , icon: 5
                                        , time: 1000
                                        , anim: 6
                                    });
                                }

                            },
                            error: function (data) {
                                layer.msg(data.msg, {
                                    offset: '15px'
                                    , icon: 5
                                    , time: 1000
                                    , anim: 6
                                });
                            }
                        });
                    });

                    submit.trigger('click');
                }
            });
        }
    });

    exports('navigation', {})
});
